package com.neusoft.neuiotms.dcvm.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.neuiotms.dcvm.bean.MonitorDataBean;
import com.neusoft.neuiotms.dcvm.service.DatabusService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api("监控数据管理")
@RestController
@RequestMapping(value = "/api/w/monitordatas")
public class MonitorController {

    @Autowired
    private DatabusService databusService;

    @ApiOperation("获取监控数据")
    @GetMapping("/asset/{code}")
    public ResultDTO<?> assetsOfLocation(@PathVariable final String code,
            @RequestParam(required = false) Map<String, Object> params) {
        if (params.containsKey("fromCache") && String.valueOf(params.get("fromCache")).equals("0")) {
            final Map<String, Object> res = this.databusService.fetchMonitorData(code, params);
            return ResultDTO.success(res);
        } else {
            final MonitorDataBean res = this.databusService.getMonitorData(code);
            return ResultDTO.success(res);
        }

    }

    @ApiOperation("根据分类获取监控数据")
    @GetMapping("/category/{category}")
    public ResultListDTO<?> monitorDatumByCategory(@PathVariable final String category) {
        final List<MonitorDataBean> res = this.databusService.getMonitorDatum(category);
        return ResultListDTO.success(res);
    }

    @ApiOperation("根据分类获取监控数据")
    @GetMapping("/category/{category}/location/{location}")
    public ResultListDTO<?> monitorDatumByCategoryAndLocation(@PathVariable final String category,
            @PathVariable final String location) {
        final List<MonitorDataBean> res = this.databusService.getMonitorDatum(category, location);
        return ResultListDTO.success(res);
    }

}
